package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.exception.BaseException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {


    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        //1.开始分页
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        //2.查询分类
        Page<Category> page = categoryMapper.findCategoryByNameAndType(categoryPageQueryDTO);
        //3.封装数据并返回
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return pageResult;
    }

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void insertCategory(CategoryDTO categoryDTO) {
        //1.将DTO转换为实体对象
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);

        //2.完善实体信息
        category.setStatus(StatusConstant.DISABLE);
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        //3.保存到数据库
        categoryMapper.insertCategory(category);

    }

    /**
     * 修改分类状态
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Long id) {
       //修改数据对应id的状态
       categoryMapper.updateStatus(status,id);
    }

    /**
     * 删除分类
     * @param id
     */
    @Override
    @Transactional
    public void delete(Long id) {
        //1.查询当前分类是否关联了菜品，如果关联了，则无法删除
        List<Dish> dishList = dishMapper.findDishByCategoryId(id);
        if(dishList != null && dishList.size() > 0){
            throw new BaseException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }else{
            //2.删除分类
            categoryMapper.delete(id);
        }
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> findCategoryByType(Integer type) {
        List<Category> category = categoryMapper.findCategoryByType(type);
        return category;
    }

    /**
     * 修改分类
     * @param category
     */
    @Override
    public void update(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
    }

    @Override
    public List<Category> findCategorysByType(Integer type) {

        return categoryMapper.findCategorysByType(type);
    }


}
